package org.createModel.factory.config_factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class CoffeeFactory {

    // 加载配置文件,生成coffee对象
    private static Map<String, Coffee> map = new ConcurrentHashMap();

    static {
        Properties p = new Properties();
        InputStream stream = CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        try {
            p.load(stream);
            Set<Object> keySet = p.keySet();
            for (Object key: keySet) {
                String className =  p.getProperty((String)key);
                // 通过反射创建对象
                Class<?> clazz = Class.forName(className);
                Coffee coffee = (Coffee)clazz.newInstance();
                map.put((String)key, coffee);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static Coffee createCoffee(String name) {
        return map.get(name);
    }

}
